using UnityEngine;
using System.Collections.Generic;

public class EE_BuildGrid_Controller : MonoBehaviour
{
    private int rows = 45;public int Rows{get { return this.rows; } }
    private int cols = 45;public int Cols{get { return this.cols; } }
    private float cellSize = 1f; public float CellSize{get { return this.cellSize; } }

    [HideInInspector] public List<EE_Building_Controller> buildings = new List<EE_Building_Controller>();
    /* 利用网格坐标获得世界坐标，建筑左下角 */
    public Vector3 GetStartPosition(int x, int y)
    {
        Vector3 position = transform.position;
        position += (transform.right.normalized * x * cellSize) + (transform.forward.normalized * y * cellSize);
        return position;
    }
    /* 利用网格坐标获得世界坐标，建筑中心 */
    /* x和y是相对于grid的坐标(网格坐标), rows和cols是该建筑占用了rows行和cols列 */
    public Vector3 GetCenterPosition(int x, int y, int rows, int cols)
    {
        Vector3 position = GetStartPosition(x, y);
        position += (transform.right.normalized * cols * cellSize / 2) + (transform.forward.normalized * rows * cellSize / 2);
        return position;
    }
    public Vector3 GetCenterPosition(EE_Building_Controller building)
    {
        return GetCenterPosition(building.CurrentX, building.CurrentY, building.Rows, building.Cols);
    }
    /* 利用网格坐标获得世界坐标，建筑右上角 */
    public Vector3 GetEndPosition(int x, int y, int rows, int cols)
    {
        Vector3 position = GetStartPosition(x, y);
        position += (transform.right.normalized * cols * cellSize) + (transform.forward.normalized * rows * cellSize);
        return position;
    }
    public Vector3 GetEndPosition(EE_Building_Controller building)
    {
        return GetEndPosition(building.CurrentX, building.CurrentY, building.Rows, building.Cols);
    }

    /* 按键按下位置在建筑大小的范围内才是移动建筑, 否则就是移动摄像机 */
    /* 3D世界plane坐标position是否在 网格坐标系的矩形(x,y,rows,cols)范围内 */
    public bool IsPlanePositionInnerRectangle(Vector3 position, int x, int y, int rows, int cols)
    {
        // Debug.Log("before inverse position: " + position);
        // 将平面坐标转为网格坐标
        position = transform.InverseTransformPoint(position);
        // 构建矩形
        Rect rect = new Rect(x, y, cols, rows);
        // Debug.Log($"after inverse position: {position},rect: (x:{x}, y:{y}, cols:{cols}, rows:{rows})");
        if (rect.Contains(new Vector2(position.x, position.z)))
        {
            return true;
        }
        return false;
    }

    public bool CanPlaceBuilding(EE_Building_Controller building, int x, int y)
    {
        if (building.CurrentX < 0 || building.CurrentY < 0 || building.CurrentX+building.Cols > cols || building.CurrentY+building.Rows > rows)
        {
            return false;
        }
        // 检查 当前建筑 是否 与已有建筑发生重叠
        for (int i = 0; i < buildings.Count; i++)
        {
            EE_Building_Controller other = buildings[i];
            if (other != building)
            {
                // Rect rect1 = new Rect(other.CurrentX, other.CurrentY, other.Cols, other.Rows);
                // Rect rect2 = new Rect(building.CurrentX, building.CurrentY, building.Cols, building.Rows);
                // // 判断两个矩形是否有重叠
                // if (rect2.Overlaps(rect1))
                // {
                //     return false;
                // }
                if (other.CurrentX < building.CurrentX + building.Cols &&
                    other.CurrentX + other.Cols > building.CurrentX &&
                    other.CurrentY < building.CurrentY + building.Rows && 
                    other.CurrentY + other.Rows > building.CurrentY)
                {
                    return false;
                }
            }
        }
        return true;
    }

#if UNITY_EDITOR
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.white;
        for (int i = 0; i <= rows; i++)
        {
            Vector3 point = transform.position + transform.forward.normalized * cellSize * i;
            Gizmos.DrawLine(point, point + transform.right.normalized * cellSize * cols);
        }
        for (int i = 0; i <= cols; i++)
        {
            Vector3 point = transform.position + transform.right.normalized * cellSize * i;
            Gizmos.DrawLine(point, point + transform.forward.normalized * cellSize * rows);
        }
    }
#endif
}